﻿using HrEFCore;
using HrEFCore.Models;
using HrService;
using Microsoft.AspNetCore.Mvc;
using System;

namespace HrUI.Controllers
{
	public class RolesController : Controller
	{
		private readonly MyDbContext myDbContext;
		private readonly RolesService rolesService;
		public RolesController(MyDbContext myDbContext, RolesService rolesService)
		{
			this.myDbContext = myDbContext;
			this.rolesService = rolesService;
		}
		public IActionResult Index()
		{
			return View();
		}
        public async Task<IActionResult> SelectFY(FenYe2<Roles> fy)
		{
			fy = await rolesService.SelectRolesByFY(fy);
			return Json(fy);
		}
		public IActionResult AddRoles()//跳添加页面
		{
			return View();
		}
		public async Task<IActionResult> Add(Roles roles) //添加
		{
			int row = await rolesService.AddRoles(roles);
			if(row > 0)
			{
				return RedirectToAction("Index");
			}
			else
			{
				return RedirectToAction("Addroles");
			}
		}
		public async Task<IActionResult> DelRoles(short id)
		{
			int row = await rolesService.DelRoles(id);
			if (row > 0)
			{
				return RedirectToAction("Index");
			}
			else
			{
				return RedirectToAction("Index");
			}
		}
		public async Task<IActionResult> UpdRoles(short id)
		{
			Roles roles = await rolesService.SelectById(id);
			ViewData["RoleId"] = id; // 将id传递给视图
			HttpContext.Session.SetInt32("RoleId",id);
			ViewData.Model = roles;
			return View();
		}
		[HttpPost]
		public async Task<IActionResult> Update2(string selectedNodeKeys)
		{
			//更新角色菜单权限
			string[] keysArray = selectedNodeKeys.Split(',');
			RolesJuri rolesJuri = new RolesJuri();

			rolesJuri.JuriID = string.Join(",", keysArray);
			rolesJuri.RolesID = (short)HttpContext.Session.GetInt32("RoleId");

			int row = rolesService.UpdateQuanXian(rolesJuri);
			if (row > 0)
			{
				return RedirectToAction("Index");
			}
			else
			{
				return RedirectToAction("UpdRoles", new { id = ViewData["RoleId"] });
			}
		}
		public IActionResult MenuTree()
		{
			List<Menu> menus = rolesService.SelectMenuBy(); //获取菜单所有数据
			return Json(menus);
		}
		public IActionResult MenuTreeById(short id)
		{
			List<Menu> menus = rolesService.SelectMenuById(id); // 根据角色ID获取菜单数据
			return Json(menus);
		}
		private List<Menu> BuildTree(List<Jurisdiction> jurisdictions, short parentId)
		{
			var children = jurisdictions.Where(e => e.GroupID == parentId);
			if (!children.Any())
			{
				return null;
			}
			return children.Select(child => new Menu()
			{
				JuriID = child.JuriID,
				JurName = child.JurName,
				GroupID = child.GroupID,
				JurAddress = child.JurAddress,
				Children = BuildTree(jurisdictions, child.JuriID)
			}).ToList();
		}
		[HttpPost]
		public async Task<IActionResult> Update(Roles roles)//修改
		{
			// 获取选中的值
			var checkedKeys = Request.Form["checkedKeys"].ToString();
            
			int row = await rolesService.UpdRoles(roles);
			if (row > 0)
			{
				return RedirectToAction("Index");
			}
			else
			{
				return RedirectToAction("UpdRoles" , new {id=roles.RolesID});
			}
		}

	}
}
